package com.yy.bootdemo.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.dto.RoleDto;
import com.yy.bootdemo.admin.entity.MyRole;
import com.yy.bootdemo.admin.service.RoleService;
import com.yy.bootdemo.security.utils.PageUtils;
import com.yy.bootdemo.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yy
 * @createTime 2022/8/1
 */
@RestController
@RequestMapping("role")
//@Api(tags = "系统：角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;


    @PostMapping("pagelist")
    @ApiOperation(value = "分页返回角色列表")
    @PreAuthorize("hasAnyAuthority('role:list')")
    //@MyLog("查询角色")
    public R pagelist(@RequestBody PageUtils params) {
        // 条件构造器
        QueryWrapper<MyRole> userQuery = Wrappers.query();
        Page<MyRole> page = new Page<>(params.getCurrent(), params.getSize());
        IPage<MyRole> userPage = roleService.page(page, userQuery);

        return R.ok(userPage);
    }





    @GetMapping("getById")
    public R getUser(@RequestParam Integer roleId){
        return R.ok(roleService.getById(roleId));
    }
    @ApiOperation(value = "添加角色")
    @PreAuthorize("hasAnyAuthority('role:add')")

    @PostMapping("save")
    public R save(@RequestBody MyRole myMenu){
        roleService.save(myMenu);
        return R.ok();
    }
/*

    @ApiOperation(value = "修改角色")
    @PreAuthorize("hasAnyAuthority('role:edit')")
    @PostMapping("updateById")
    public R updateById(@RequestBody MyRole myRole){
        roleService.updateById(myRole);
        return R.ok();
    }
*/



    @ApiOperation(value = "修改角色")
    @PreAuthorize("hasAnyAuthority('role:edit')")
    @PostMapping("updateById")
    public R updateById(@RequestBody RoleDto roleDto){
        roleService.update(roleDto);
        return R.ok();
    }


    @PutMapping(value = "/authDataScope")
    @ApiOperation(value = "修改角色数据权限")
    public R updateauthDataScope(@RequestBody RoleDto roleDto) {

         // roleService.authDataScope(roleDto);
        return R.ok();
    }



    @DeleteMapping
    @ApiOperation(value = "删除角色")
    @PreAuthorize("hasAnyAuthority('role:del')")
    public R<MyRole> deleteRole(RoleDto roleDto) {
          roleService.delete(roleDto.getRoleId());
        return R.ok();
    }

    @GetMapping("/getList")
    @ApiOperation(value = "角色列表")
    @PreAuthorize("hasAnyAuthority('user:list')")
    public R<MyRole> getAll(){



        List<MyRole> list=  roleService.list();
        return R.ok(list);
    }
}
